﻿
// Global Variables.
var map = null;
var ucSearchResultsMap = null; // This global variable is here to allow all the other AJAX enabled controls to interact with this one.

(function($) {

    window["load_BingMapsSample_ucSearchResultsMap"] = function(obj) {
        
        
        // Variables.
        ucSearchResultsMap = obj;
        ucSearchResultsMap.Places = null;
        
        
        // Initialize the map.
        map = new VEMap("search_results_map");
        map.LoadMap();
        map.SetMouseWheelZoomToCenter(false);
        
        mapContainer = $(".map_container");
        mapContainer.removeClass("loading");
        
        var mainLayer = new VEShapeLayer();
        mainLayer.SetTitle("Main Layer");
        map.AddShapeLayer(mainLayer);
        
        
        
        // Event Handlers.
        var onMouseOverHandler = function(mapEvent) {
            if (mapEvent.elementID)
            {
                var shape = map.GetShapeByID(mapEvent.elementID);
                
                if (shape.Place)
                {
                    if (shape.GetDescription().length == 0)
                    {
                        ucSearchResultsMap.setPlaceDescription(shape);
                    }
                }
            }
        };
                
        
        
        // Functions.
        ucSearchResultsMap.attachMapEvents = function() {
            map.AttachEvent("onmouseover", onMouseOverHandler);
        };
        
        ucSearchResultsMap.attachMapEvents();
        
        
        ucSearchResultsMap.detachMapEvents = function() {
            map.DetachEvent("onmouseover", onMouseOverHandler);
        };
        
        
        ucSearchResultsMap.clearPlaces = function() {
            mainLayer.DeleteAllShapes();
            ucSearchResultsMap.Places = null;
        };
        
        
        ucSearchResultsMap.addPlaces = function(places) {
            if (!places || !places.length || places.length == 0) { return; }
            ucSearchResultsMap.Places = [];
            
            for (var index = 0; index < places.length; index++)
            {
                var placeShape = new VEShape(VEShapeType.Pushpin, new VELatLong(places[index].Location.Coordinate.Latitude, places[index].Location.Coordinate.Longitude));
                
                placeShape.Place = places[index];
                ucSearchResultsMap.Places[ucSearchResultsMap.Places.length] = places[index];
                
                mainLayer.AddShape(placeShape);
            }
            
            map.SetMapView(mainLayer.GetBoundingRectangle());
        };
        
        
        ucSearchResultsMap.setPlaceDescription = function(shape) {
            ucSearchResultsMap.GlobalInvoke("BingMapsSample.ucPlaceDescription, BingMapsSample", "RenderPlaceDescription", {
                Params: [shape.Place.Name],
                ReturnHtml: true,
                OnSuccess: function(result) {
                    shape.SetDescription(result.Html);
                    map.ShowInfoBox(shape);
                }
            });
        };
        
        
        
        // Load the first page of search results.
        if (initialLoadHandler)
        {
            initialLoadHandler();
        }
    };

})(jQuery.noConflict());